window._agl            = window._agl || [];
window._uc_transfer_id = null;

var loadOceanTransferCode = function (accountID, transferID) {
    (function (r, d, s, l) {
        var meteor     = r.meteor = r.meteor || [];
        meteor.methods = [ "track", "off", "on" ];
        meteor.factory = function (method) {
            return function () {
                var args = Array.prototype.slice.call(arguments);
                args.unshift(method);
                meteor.push(args);
                return meteor
            }
        };
        for (var i = 0 ; i < meteor.methods.length ; i++) {
            var key       = meteor.methods[ i ];
            meteor[ key ] = meteor.factory(key)
        }
        meteor.load = function () {
            var js,
                fjs = d.getElementsByTagName(s)[ 0 ];
            js      = d.createElement(s);
            js.src  = "https://analytics.snssdk.com/meteor.js/v1/" + l + "/sdk";
            fjs.parentNode.insertBefore(js, fjs)
        };
        meteor.load();
        if (meteor.invoked) {
            return
        }
        meteor.invoked = true;
        meteor.track("pageview")
    })(window, document, "script", accountID);
    window._today_online_transfer_id = transferID;
};

var loadUcTransferCode = function (transferID) {
    (function (w, d, t, s, q, m, n) {
        if (w.utq) return;
        q       = w.utq = function () {
            q.process ? q.process(arguments) : q.queue.push(arguments);
        };
        q.queue = [];
        m       = d.getElementsByTagName(t)[ 0 ];
        n       = d.createElement(t);
        n.src   = s;
        n.async = true;
        m.parentNode.insertBefore(n, m);
    })(window, document, 'script', 'https://image.uc.cn/s/uae/g/0s/ad/utracking.js');
    utq('set', 'convertMode', true);
    utq('set', 'trackurl', 'huichuan.sm.cn/lp');
    window._uc_transfer_id = transferID;
};

var loadBaiduTransferCode = function (production) {
    _agl.push(
        [ 'production', production ]
    );
    (function () {
        var agl   = document.createElement('script');
        agl.type  = 'text/javascript';
        agl.async = true;
        agl.src   = 'https://fxgate.baidu.com/angelia/fcagl.js?production=' + production;
        var s     = document.getElementsByTagName('script')[ 0 ];
        s.parentNode.insertBefore(agl, s);
    })();
};

(function () {
    var host = window.location.host;

    var production            = null;
    var ucProduction          = null;
    var todayOnlineProduction = null;

    if (/bd\.xxl/.test(host))
        production = '_f7L2XwGXjyszb4d1e2oxPybgD';

    if (/uc\.xxl/.test(host))
        ucProduction = true;

    if (/tt\.xxl/.test(host) || /dy\.xxl/.test(host))
        todayOnlineProduction = true;


    if (todayOnlineProduction || ucProduction) {
        var url        = new URL(window.location.href);
        var transferId = url.searchParams.get("transferid");
        var accountId  = url.searchParams.get("accountid");

        (todayOnlineProduction && transferId) && loadOceanTransferCode(accountId, transferId);
        (ucProduction && transferId) && loadUcTransferCode(transferId);
    }

    production && loadBaiduTransferCode(production);

})();

function $_aglPush() {
    if (window._agl && typeof window._agl.push === 'function') {
        window._agl.push([ 'track', [ 'success', { t: 3 } ] ])
    }

    if (window.utq && typeof utq === 'function') {
        utq('track', 'FormSubmit', window._uc_transfer_id);
    }

    if (window.meteor && typeof meteor.track === 'function') {
        meteor.track("form", { convert_id: window._today_online_transfer_id })
    }
}
